//
//  QuestionCell.m
//  suRead
//
//  Created by nancy  on 16/9/13.
//  Copyright © 2016年 bao. All rights reserved.
//

#import "QuestionCell.h"
#import "M_Question.h"
#import "FMDBHelpers.h"
#import "UIImageView+WebCache.h"

@interface QuestionCell()

@property (weak, nonatomic) IBOutlet UIImageView *userPhoto;
@property (weak, nonatomic) IBOutlet UILabel *nameLable;
@property (weak, nonatomic) IBOutlet UILabel *askContent;
@property (weak, nonatomic) IBOutlet UILabel *asktimeLable;
@property (weak, nonatomic) IBOutlet UILabel *labelCommentCount;
@property (weak, nonatomic) IBOutlet UIImageView *icon_comment;
@property (weak, nonatomic) IBOutlet UIImageView *zhuanjiaPhoto;
@property (weak, nonatomic) IBOutlet UILabel *zhuanjianameLable;
@property (weak, nonatomic) IBOutlet UILabel *replyContent;
@property (weak, nonatomic) IBOutlet UILabel *replytimeLable;
@property (weak, nonatomic) IBOutlet UILabel *labelSupportCount;

@end

@implementation QuestionCell

+ (CGFloat)heightWithContent:(M_Question *)questionData_
{

    
    CGFloat screenWidth = [UIScreen mainScreen].bounds.size.width;
    
    //顶部高度 + 底部高度
    CGFloat height_ = 150.0f;
    
    //用户名称高度
    CGFloat usernameHeight = [questionData_.f_ask_user_name boundingRectWithSize:CGSizeMake(screenWidth - 80.0f - 55.0f, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:17.0f]} context:nil].size.height;
    height_ += usernameHeight;
    
    
    //问题高度
    NSRange range = [questionData_.f_ask_content rangeOfString:@"</span>"];//判断字符串是否包含
    if (range.location !=NSNotFound)
    {
        NSString *jsString                 = [NSString stringWithFormat:@"<style type=\"text/css\"> \n"
                                              "span,pre{color: #9e9e9f;font-size: .8rem;}"
                                              "</style> \n"
                                              "%@ \n",questionData_.f_ask_content];
        NSAttributedString * attrStr       = [[NSAttributedString alloc] initWithData:[jsString dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
        
        CGFloat ask_contentHeight  = [attrStr boundingRectWithSize:CGSizeMake(screenWidth - 20.0f - 15.0f, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading context:nil].size.height;
        
        
        height_ += ask_contentHeight;
    }
    else {
        
        CGFloat ask_contentHeight = [questionData_.f_ask_content boundingRectWithSize:CGSizeMake(screenWidth - 20.0f - 15.0f, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:16.0f]} context:nil].size.height;
        height_ += ask_contentHeight;
    }
    
    //答案高度
    NSRange range2 = [questionData_.f_reply_content rangeOfString:@"</span>"];//判断字符串是否包含
    if (range2.location !=NSNotFound)
    {
        NSString *jsString                 = [NSString stringWithFormat:@"<style type=\"text/css\"> \n"
                                              "span,pre{color: #9e9e9f;font-size: .8rem;}"
                                              "</style> \n"
                                              "%@ \n",questionData_.f_reply_content];
        NSAttributedString * attrStr       = [[NSAttributedString alloc] initWithData:[jsString dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
        
        CGFloat reply_contentHeight  = [attrStr boundingRectWithSize:CGSizeMake(screenWidth - 20.0f - 15.0f, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading context:nil].size.height;
        
        
        height_ += reply_contentHeight;
    }
    else {
        
        CGFloat reply_contentHeight = [questionData_.f_reply_content boundingRectWithSize:CGSizeMake(screenWidth - 20.0f - 15.0f, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14.0f]} context:nil].size.height;
        height_ += reply_contentHeight;
    }
    return height_;
}

- (void)setCellData:(id)cellData_
{
    M_Question *cellData = cellData_;
    
    self.userPhoto.layer.cornerRadius  = 20.0;//设置图片圆角的尺度
    self.userPhoto.layer.masksToBounds = YES;//没这句话它圆不起来
    //    self.userPhoto.image = [UIImage imageNamed:@"icon_user"];
    [self.userPhoto sd_setImageWithURL:[NSURL URLWithString:cellData.f_ask_user_img] placeholderImage:[UIImage imageNamed:@"icon_user"]];
    
    self.nameLable.text = cellData.f_ask_user_name;
    
    //专家信息
    self.zhuanjiaPhoto.layer.cornerRadius  = 20.0;//设置图片圆角的尺度
    self.zhuanjiaPhoto.layer.masksToBounds = YES;//没这句话它圆不起来
    [self.zhuanjiaPhoto sd_setImageWithURL:[NSURL URLWithString:cellData.f_zhuanjia_img] placeholderImage:[UIImage imageNamed:@"icon_user"]];
    
    self.zhuanjianameLable.text = cellData.f_zhuanjia_name;
    
    
    NSRange range = [cellData.f_ask_content rangeOfString:@"</span>"];//判断字符串是否包含
    if (range.location !=NSNotFound)
    {
        
        NSString *jsString                 = [NSString stringWithFormat:@"<style type=\"text/css\"> \n"
                                              "span,pre{color: #9e9e9f;font-size: .8rem;}"
                                              "</style> \n"
                                              "%@ \n",cellData.f_ask_content];
        NSAttributedString * attrStr       = [[NSAttributedString alloc] initWithData:[jsString dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
        
        self.askContent.attributedText = attrStr;
        
    } else {
        self.askContent.text = cellData.f_ask_content;
    }
    
    
    
    self.asktimeLable.text = cellData.f_ask_time;
    self.labelCommentCount.text = cellData.f_ask_comment_count;
    
    _icon_comment.userInteractionEnabled = YES;
    UITapGestureRecognizer *tapGesturRecognizer=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(toCommentList)];
    [_icon_comment addGestureRecognizer:tapGesturRecognizer];
    
    
    
    NSRange range2 = [cellData.f_reply_content rangeOfString:@"</span>"];//判断字符串是否包含
    if (range2.location !=NSNotFound)
    {
        
        NSString *jsString                 = [NSString stringWithFormat:@"<style type=\"text/css\"> \n"
                                              "span,pre{color: #9e9e9f;font-size: .8rem;}"
                                              "</style> \n"
                                              "%@ \n",cellData.f_reply_content];
        NSAttributedString * attrStr       = [[NSAttributedString alloc] initWithData:[jsString dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
        
        self.replyContent.attributedText = attrStr;
        
    } else {
        self.replyContent.text = cellData.f_reply_content;
    }
    
    
    
    self.replytimeLable.text = cellData.f_reply_time;
    self.labelSupportCount.text = cellData.f_reply_square;

}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}



- (void)toCommentList
{
    if(self.commentlistClickCallBack){
        self.commentlistClickCallBack();
    }
}

@end